package com.cpi.cornpp.bushelversion.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.bushelversion.exception.BushelsVersionException;
import com.cpi.cornpp.bushelversion.form.BushelsVersionForm;
import com.cpi.cornpp.bushelversion.services.BushelsVersionService;
import com.cpi.cornpp.bushelversion.view.BushelsByCornOriginVersionView;
import com.cpi.cornpp.common.exception.CornPPException;
import com.cs.common.utils.NumberUtil;

/**
 * Bushels Versions Action
 * @author <i> Edgar Joao </i>
 * @version 1.0
 * @see MappingDispatchAction
 */
public class BushelsVersionAction extends MappingDispatchAction {
	/** Default forward */
	private final static String DEFAULT_FORWARD = "default";//$NON-NLS-1$
	/** Custom exception */
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	/** Id de version */
	private final static String VERSION_ID_LABEL = "versionId";//$NON-NLS-1$
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	/** Success edit key */
	private static final String SUCCESS_EDIT_LABEL = "common.edit.success";//$NON-NLS-1$
	private static final String BUSHELS_VERSION_GROUP_VIEW = "BushelsVersionGroupsView";//$NON-NLS-1$
	/**
	 * Muestra el listado de Bushels Versions
	 * 
	 * @author Edgar Joao
	 * @param argMapping
	 * @param argForm
	 * @param argRequest
	 * @param argResponse
	 * @return
	 * @throws Exception
	 */
	public ActionForward showBushelsVersions(ActionMapping argMapping, ActionForm argForm, HttpServletRequest argRequest, HttpServletResponse argResponse)throws BushelsVersionException {
		HttpSession httpSession = null;
		BushelsVersionService bushelsVersionService = null;
		int versionId = 0;
		List bushelsVersionGroupsView = null;
		BushelsVersionForm bushelsVersionForm = null;
		try{
			httpSession = argRequest.getSession();
			bushelsVersionForm = (BushelsVersionForm)argForm;
			versionId = NumberUtil.getInt(httpSession.getAttribute(VERSION_ID_LABEL));
			bushelsVersionService = new BushelsVersionService();
			bushelsVersionGroupsView = bushelsVersionService.getBushelsVersions(versionId);
			httpSession.setAttribute(BUSHELS_VERSION_GROUP_VIEW, bushelsVersionGroupsView);
			if( bushelsVersionForm == null ){
				bushelsVersionForm = new BushelsVersionForm();
			}
			bushelsVersionForm.setBushelsVersionGroupsView(bushelsVersionGroupsView);
		}catch (BushelsVersionException bushelsVersionException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, bushelsVersionException);
			throw bushelsVersionException;
		}catch (Exception exception){
			BushelsVersionException bushelsVersionException = null;
			exception.printStackTrace(System.out);
			bushelsVersionException= new BushelsVersionException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			httpSession.setAttribute(CUSTOM_EXCEPTION, bushelsVersionException);
			throw bushelsVersionException;
		}
		return argMapping.findForward(DEFAULT_FORWARD);			
	}
	/**
	 * Save Bushels Version
	 * 
	 * @author Edgar Joao	
	 * @param argMapping
	 * @param argForm
	 * @param argRequest
	 * @param argResponse
	 * @return
	 * @throws Exception
	 */
	public ActionForward saveBushelsVersion(ActionMapping argMapping, ActionForm argForm,HttpServletRequest argRequest, HttpServletResponse argResponse)throws BushelsVersionException {
		HttpSession httpSession = null;
		BushelsVersionService bushelsVersionService = null;
		BushelsVersionForm bushelsVersionForm = null;
		MessageResources messageResources = null;
		String messageError = null;
		try{
			httpSession = argRequest.getSession();
			bushelsVersionForm = (BushelsVersionForm)argForm;
			bushelsVersionService = new BushelsVersionService();
			messageResources = getResources(argRequest);
			bushelsVersionService.updatePlantsVersions(concatList(bushelsVersionForm.getBushelsVersionGroupsView()));
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_EDIT_LABEL);
			httpSession.setAttribute(MESSAGE, messageError);
		}catch (BushelsVersionException bushelsVersionException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, bushelsVersionException);
			throw bushelsVersionException;
		}catch (Exception exception){
			BushelsVersionException bushelsVersionException = null;
			exception.printStackTrace(System.out);
			bushelsVersionException= new BushelsVersionException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_UPDATE);
			httpSession.setAttribute(CUSTOM_EXCEPTION, bushelsVersionException);
			throw bushelsVersionException;
		}
		return argMapping.findForward(DEFAULT_FORWARD);
	}
	
	private List concatList(List bushelsVersionGroupsView)throws BushelsVersionException{
		List bushelsVersionViews = null;
		Iterator iterator = null;
		BushelsByCornOriginVersionView bushelsByCornOriginVersionView = null;
		try{
			iterator = bushelsVersionGroupsView.iterator();
			bushelsVersionViews = new ArrayList(0);
			while(iterator.hasNext()){
				bushelsByCornOriginVersionView= (BushelsByCornOriginVersionView)iterator.next();
				bushelsVersionViews.addAll(bushelsByCornOriginVersionView.getBushelsVersionViews());
			}
		}catch(Exception exception){
			BushelsVersionException bushelsVersionException = null;
			exception.printStackTrace(System.out);
			bushelsVersionException= new BushelsVersionException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_UPDATE);
			throw bushelsVersionException;
		}
		return bushelsVersionViews;
	}
}